/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rotatepolygon;

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;

/**
 *
 * @author shohdi
 */
public class RotatePolygon extends JFrame implements KeyListener,MouseListener,MouseMotionListener  {

    private Polygon poly = null;
    
    private int rotation = 0;
    
    
    private int [] xPoints = {0,-10,-7,7,10};
    private int [] yPoints = {-10,-2,10,10,-2};
    
    private int x ;
    private int y ;
    
    
    
    
    
    public RotatePolygon()
    {
        super("Rotate Polygon");
        setSize(640,480);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        init();
        
    }   
    
    
    
    
    public void init()
    {
        setSize(640,480);
        
        addKeyListener(this);
        addMouseListener(this);
        addMouseMotionListener(this);
        
        
        poly  = new  Polygon(xPoints,yPoints,xPoints.length);
        
        
    }
    
    public void paint(Graphics g)
    {
        Graphics2D g2D = (Graphics2D)g;
        
        int width = getSize().width;
        int height = getSize().height;
        
        AffineTransform identity = new AffineTransform();
        
        g2D.setTransform(identity);
        
        g2D.setColor(Color.BLACK);
        
        g2D.fill(new Rectangle( 0,0,width,height));
        
        g2D.setTransform(identity);
        
        g2D.translate(width/2, height/2);
        
        g2D.scale(20, 20);
        
        g2D.rotate(Math.toRadians(rotation));
        
        
        
        
        
        g2D.setColor(Color.RED);
        g2D.fill(poly);
        g2D.setColor(Color.BLUE);
        g2D.draw(poly);
        
        
        
        
    }
    
    
    public void mouseDragged(MouseEvent m)
    {
        
    }
    
    
    public void mouseMoved(MouseEvent m)
    {
        int DeltaX = m.getX() - x;
        int DeltaY = m.getY() - y;
        
        x = m.getX();
        y = m.getY();
        
        if (DeltaX > 0)
        {
            rotation ++;
                rotation = rotation % 360;
                repaint();
        }
        else if (DeltaX<0)
        {
             rotation--;
                if (rotation < 0)
                    rotation = 360 + rotation;
                rotation = rotation % 360;
                repaint();
        }
            
            
    }
    
    public void mousePressed (MouseEvent m)
    {
        switch (m.getButton())
        {
            case MouseEvent.BUTTON1:
                rotation--;
                if (rotation < 0)
                    rotation = 360 + rotation;
                rotation = rotation % 360;
                repaint();
                break;
            case MouseEvent.BUTTON3:
                rotation++;
                
                rotation = rotation % 360;
                repaint();
                break;
        }
    }
    
    
    public void mouseClicked (MouseEvent m)
    {
        
    }
    
    public void mouseReleased(MouseEvent m)
    {
    
    }
    
    
    public void mouseEntered(MouseEvent m)
    {
      
    }
    
    public void mouseExited (MouseEvent m)
    {}
    
    
    public void keyPressed(KeyEvent e)
    {
        switch (e.getKeyCode())
        {
            case KeyEvent.VK_LEFT:
                rotation--;
                if (rotation < 0)
                    rotation = 360 + rotation;
                rotation = rotation % 360;
                repaint();
                break;
            case KeyEvent.VK_RIGHT:
                rotation ++;
                rotation = rotation % 360;
                repaint();
                break;
                
        }
    }
    
    
    public void keyTyped (KeyEvent e)
    {}
    
    
    
    public void keyReleased(KeyEvent e)
    {
        
    }
    
    
    
    /**
     * @param args the command line arguments
     */
    
    
    
    
    
    public static void main(String[] args) {
        
        
        RotatePolygon pol = new  RotatePolygon();
        
        
        // TODO code application logic here
    }
}
